#ifndef THINK_THINK_MODEL_RELATION_H
#define THINK_THINK_MODEL_RELATION_H

static char* think_model_relation =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model;\n"
	"\n"
	"use think\\db\\Query;\n"
	"use think\\Exception;\n"
	"use think\\Model;\n"
	"\n"
	"/**\n"
	" * Class Relation\n"
	" * @package think\\model\n"
	" *\n"
	" * @mixin Query\n"
	" */\n"
	"abstract class Relation\n"
	"{\n"
	"    // 父模型对象\n"
	"    protected $parent;\n"
	"    /** @var  Model 当前关联的模型类 */\n"
	"    protected $model;\n"
	"    /** @var Query 关联模型查询对象 */\n"
	"    protected $query;\n"
	"    // 关联表外键\n"
	"    protected $foreignKey;\n"
	"    // 关联表主键\n"
	"    protected $localKey;\n"
	"    // 基础查询\n"
	"    protected $baseQuery;\n"
	"    // 是否为自关联\n"
	"    protected $selfRelation;\n"
	"\n"
	"    /**\n"
	"     * 获取关联的所属模型\n"
	"     * @access public\n"
	"     * @return Model\n"
	"     */\n"
	"    public function getParent()\n"
	"    {\n"
	"        return $this->parent;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取当前的关联模型对象实例\n"
	"     * @access public\n"
	"     * @return Model\n"
	"     */\n"
	"    public function getModel()\n"
	"    {\n"
	"        return $this->query->getModel();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取关联的查询对象\n"
	"     * @access public\n"
	"     * @return Query\n"
	"     */\n"
	"    public function getQuery()\n"
	"    {\n"
	"        return $this->query;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置当前关联为自关联\n"
	"     * @access public\n"
	"     * @param  bool $self 是否自关联\n"
	"     * @return $this\n"
	"     */\n"
	"    public function selfRelation($self = true)\n"
	"    {\n"
	"        $this->selfRelation = $self;\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 当前关联是否为自关联\n"
	"     * @access public\n"
	"     * @return bool\n"
	"     */\n"
	"    public function isSelfRelation()\n"
	"    {\n"
	"        return $this->selfRelation;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 封装关联数据集\n"
	"     * @access public\n"
	"     * @param array $resultSet 数据集\n"
	"     * @return mixed\n"
	"     */\n"
	"    protected function resultSetBuild($resultSet)\n"
	"    {\n"
	"        return (new $this->model)->toCollection($resultSet);\n"
	"    }\n"
	"\n"
	"    protected function getQueryFields($model)\n"
	"    {\n"
	"        $fields = $this->query->getOptions(\'field\');\n"
	"        return $this->getRelationQueryFields($fields, $model);\n"
	"    }\n"
	"\n"
	"    protected function getRelationQueryFields($fields, $model)\n"
	"    {\n"
	"        if ($fields) {\n"
	"\n"
	"            if (is_string($fields)) {\n"
	"                $fields = explode(\',\', $fields);\n"
	"            }\n"
	"\n"
	"            foreach ($fields as &$field) {\n"
	"                if (false === strpos($field, \'.\')) {\n"
	"                    $field = $model . \'.\' . $field;\n"
	"                }\n"
	"            }\n"
	"        } else {\n"
	"            $fields = $model . \'.*\';\n"
	"        }\n"
	"\n"
	"        return $fields;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行基础查询（仅执行一次）\n"
	"     * @access protected\n"
	"     * @return void\n"
	"     */\n"
	"    protected function baseQuery()\n"
	"    {}\n"
	"\n"
	"    public function __call($method, $args)\n"
	"    {\n"
	"        if ($this->query) {\n"
	"            // 执行基础查询\n"
	"            $this->baseQuery();\n"
	"\n"
	"            $result = call_user_func_array([$this->query, $method], $args);\n"
	"            if ($result instanceof Query) {\n"
	"                return $this;\n"
	"            } else {\n"
	"                $this->baseQuery = false;\n"
	"                return $result;\n"
	"            }\n"
	"        } else {\n"
	"            throw new Exception(\'method not exists:\' . __CLASS__ . \'->\' . $method);\n"
	"        }\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif